const sequelize = require('../config/database')

async function checkDatabase() {
  try {
    await sequelize.authenticate()
    console.log('数据库连接成功')
    
    // 检查当前使用的数据库
    const [dbResult] = await sequelize.query("SELECT DATABASE() as current_db")
    console.log(`当前数据库: ${dbResult[0].current_db}`)
    
    // 检查所有数据库
    const [databases] = await sequelize.query("SHOW DATABASES")
    console.log('\n可用数据库:')
    databases.forEach(db => {
      console.log(`- ${db.Database}`)
    })
    
    // 检查products表是否存在
    try {
      const [results] = await sequelize.query("DESCRIBE products")
      console.log('\nproducts表结构:')
      results.forEach(column => {
        console.log(`- ${column.Field}: ${column.Type} ${column.Null === 'YES' ? '(可空)' : '(非空)'} ${column.Key ? `[${column.Key}]` : ''}`)
      })
      
      // 检查是否有数据
      const [countResult] = await sequelize.query("SELECT COUNT(*) as count FROM products")
      console.log(`\nproducts表记录数: ${countResult[0].count}`)
    } catch (tableError) {
      console.log('\nproducts表不存在或无法访问')
    }
    
    // 检查print_ad_system数据库是否存在
    try {
      const [printDbResults] = await sequelize.query("USE print_ad_system; DESCRIBE products")
      console.log('\nprint_ad_system数据库中的products表结构:')
      printDbResults.forEach(column => {
        console.log(`- ${column.Field}: ${column.Type} ${column.Null === 'YES' ? '(可空)' : '(非空)'} ${column.Key ? `[${column.Key}]` : ''}`)
      })
    } catch (printDbError) {
      console.log('\nprint_ad_system数据库不存在或无法访问')
    }
    
  } catch (error) {
    console.error('检查数据库失败:', error)
  } finally {
    await sequelize.close()
  }
}

checkDatabase()